package com.too.demo.message;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
 * mq生产者
 * @author ouyf
 * @date 2024/9/4 17:12
 * <p>
 * Copyright (c) Siemens Mobility TBDS 2024, All Rights Reserved, Confidential
 */

/**
 * @author xiaobo
 */
@Service
@RequiredArgsConstructor
public class MessageProducer {

    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedisStreamUtil redisStreamUtil;


    public void sendMessage(String streamKey, String messageKey, Object message) {
        Map<String, Object> messageMap = new HashMap<>();
        messageMap.put(messageKey, message);

        /*RecordId recordId = redisTemplate.opsForStream().add(streamKey, messageMap);
        if (recordId != null) {
            System.out.println("Message sent to Stream '" + streamKey + "' with RecordId: " + recordId);
        }*/

        String messageId = redisStreamUtil.addMap(streamKey, messageMap);
        System.out.println("Message sent to Stream '" + streamKey + "' with RecordId: " + messageId);
    }
}


